package views;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.*;

import controllers.Driver;
import controllers.TimeSlot;
import db.DriverDAO;
import db.TimeSlotDAO;

public class AddTimeSlotFrame extends JFrame {
	
	private static final int WIDTH=500;
	private static final int HEIGHT=350;
	private static final String TITLE = "Add Time Slot";
	private JComboBox weekDaysMenu, startTimeHourMenu, startTimeMinuteMenu, 
	endTimeHourMenu, endTimeMinuteMenu, driverMenu, startam, endam;
	private JLabel startTimeLabel, endTimeLabel, driverLabel;
	private static final String[] weekDays = {"Monday", "Tuesday", "Wednesday", "Thursday",
			"Friday", "Saturday", "Sunday"};
	private static List<Driver> drivers;
	private static SimpleDateFormat sdf = new SimpleDateFormat("hh:mma");
	
	public AddTimeSlotFrame(int xBound, int yBound){
		this.setTitle(TITLE);
		this.setResizable(false);
		this.setBounds(xBound+50, yBound+50, WIDTH, HEIGHT);
		
		Container c = this.getContentPane();
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		
		weekDaysMenu = new JComboBox();
		weekDaysMenu.setBounds(25, 25, 200, 25);
		setWeekDaysMenu();
		weekDaysMenu.setSelectedIndex(0);
		panel.add(weekDaysMenu);
		
		startTimeLabel = new JLabel("Start Time");
		startTimeLabel.setBounds(25, 60, 200, 25);
		panel.add(startTimeLabel);
		
		startTimeHourMenu = new JComboBox();
		startTimeHourMenu.setBounds(25, 85, 50, 25);
		setTimeHourMenu(startTimeHourMenu);
		startTimeHourMenu.setSelectedIndex(0);
		panel.add(startTimeHourMenu);
		
		startTimeMinuteMenu = new JComboBox();
		startTimeMinuteMenu.setBounds(80, 85, 50, 25);
		setTimeMinuteMenu(startTimeMinuteMenu);
		startTimeMinuteMenu.setSelectedIndex(0);
		panel.add(startTimeMinuteMenu);
		
		startam = new JComboBox();
		startam.setBounds(135, 85, 50, 25);
		startam.addItem("AM");
		startam.addItem("PM");
		startam.setSelectedIndex(0);
		panel.add(startam);
		
		endTimeLabel = new JLabel("End Time");
		endTimeLabel.setBounds(25, 120, 200, 25);
		panel.add(endTimeLabel);
		
		endTimeHourMenu = new JComboBox();
		endTimeHourMenu.setBounds(25, 145, 50, 25);
		setTimeHourMenu(endTimeHourMenu);
		endTimeHourMenu.setSelectedIndex(0);
		panel.add(endTimeHourMenu);
		
		endTimeMinuteMenu = new JComboBox();
		endTimeMinuteMenu.setBounds(80, 145, 50, 25);
		setTimeMinuteMenu(endTimeMinuteMenu);
		endTimeMinuteMenu.setSelectedIndex(0);
		panel.add(endTimeMinuteMenu);
		
		endam = new JComboBox();
		endam.setBounds(135, 145, 50, 25);
		endam.addItem("AM");
		endam.addItem("PM");
		endam.setSelectedIndex(0);
		panel.add(endam);
		
		driverLabel = new JLabel("Driver");
		driverLabel.setBounds(25, 175, 200, 25);
		panel.add(driverLabel);
		
		driverMenu = new JComboBox();
		driverMenu.setBounds(25, 200, 200, 25);
		setDriverMenu();
		driverMenu.setSelectedIndex(0);
		panel.add(driverMenu);
		
		JButton save = new JButton("Save");
		save.setBounds(25, 240, 100, 30);
		save.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String weekDay = weekDaysMenu.getSelectedItem().toString();
				String driverName = driverMenu.getSelectedItem().toString();
/*				int startHour = Integer.parseInt(startTimeHourMenu.getSelectedItem().toString());
				int startMin = Integer.parseInt(startTimeMinuteMenu.getSelectedItem().toString());
				int endHour = Integer.parseInt(endTimeHourMenu.getSelectedItem().toString());
				int endMinute = Integer.parseInt(endTimeMinuteMenu.getSelectedItem().toString());*/
				int driverId = drivers.get(driverMenu.getSelectedIndex()).getId();
/*				Time startTime = new Time(startHour, startMin, 0);
				Time endTime = new Time(endHour, endMinute, 0);*/
				Date start = null;
				Date end = null;
				try {
					start = sdf.parse(startTimeHourMenu.getSelectedItem() +":"+startTimeMinuteMenu.getSelectedItem()+startam.getSelectedItem());
					end = sdf.parse(endTimeHourMenu.getSelectedItem() +":"+endTimeMinuteMenu.getSelectedItem()+endam.getSelectedItem());
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				TimeSlot ts = new TimeSlot(driverId, weekDay, new Time(start.getTime()), new Time(end.getTime()), driverName);
				TimeSlotDAO.addTimeSlot(ts);
				ViewTimeSlotsFrame.updateRows();
				AddTimeSlotFrame.this.dispose();
			}
		});
		panel.add(save);
		
		c.add(panel);
		
		this.setVisible(true);
		
	}
	
	public void setWeekDaysMenu(){
		for(int i=0;i<weekDays.length;i++){
			weekDaysMenu.addItem(weekDays[i]);
		}
	}
	
	public void setTimeHourMenu(JComboBox b){
		for(int i=1; i<13; i++){
			b.addItem(i);
		}
	}
	public void setTimeMinuteMenu(JComboBox b){
		for(int i=0; i<60; i++){
			if(i<10){
			b.addItem("0"+i);
			}
			else{
				b.addItem(i);
			}
		}
	}
	
	public void setDriverMenu(){
		drivers = DriverDAO.getDrivers();
		for(int i=0;i<drivers.size();i++){
			driverMenu.addItem(drivers.get(i));
		}
	}
	

}
